<template>
    <div>
        <Alert :title="title" :type="type" ref="alert" @close="close" />
        <div class="s-nav">
            <div class="l-row">
                <div class="logo"></div>
            </div>
        </div>
        <div class="l-row">
            <h2 class="title title_user title_mb_100">欢迎登录</h2>
        </div>
        <el-row v-if="flag">
            <el-col :span="6" :offset="9">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="" class="demo-ruleForm mt40">
                    <el-form-item label="" prop="user">
                        <el-input v-model="ruleForm.user" auto-complete="off" placeholder="请输入用户名"></el-input>
                    </el-form-item>
                    <el-form-item label="" prop="password" style="margin: 30px 0px;">
                        <el-input type="password" v-model="ruleForm.password" auto-complete="off" placeholder="请输入密码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('ruleForm')">立即提交</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <el-row v-if="!flag" class="mt20">
            <el-col :span="6" :offset="9">
                <router-link to="/dashboard" tag="button" type="primary" class="el-button el-button--primary">进入后台</router-link>
            </el-col>
        </el-row>
        
    </div>
</template>

<script>
    import Alert from '@/base/alert/index'
    export default {
        data() {
            return {
                flag: true,
                ruleForm: {
                    user: '',
                    password: ''
                },
                rules: {
                    user: [{
                        required: true,
                        message: '请输入用户名',
                        trigger: 'blur'
                    }],
                    password: [{
                        required: true,
                        message: '请输入密码',
                        trigger: 'blur'
                    }]
                },
                title: '',
                type: ''
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post('/api/users/authenticate_user', {
                            username: this.ruleForm.user,
                            password: this.ruleForm.password
                        }).then((res) => {
                            console.log(res)
                            if (res.data.code == 0) {
                                window.localStorage.setItem('token', res.data.token.auth_token)
                                this.$router.push({
                                    path: '/dashboard'
                                })
                            } else if (res.data.code == 2) {
                                console.log(this.$refs.alert)
                                this.$refs.alert.$el.style.display = 'block'
                                this.title = res.data.msg
                                this.type = 'error'
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            close() {
                this.$refs.alert.$el.style.display = 'none'
            }
        },
        beforeRouteEnter(to, from, next) {
            if (to.fullPath == '/') {
                console.log(1111)
                if (window.localStorage.getItem('token')) {
                    next(vm => {
                        vm.flag = false
                    })
                } else {
                   next(vm => {
                        vm.flag = true
                    })
                }
            }
        },
        components: {
            Alert
        }
    }
</script>

<style scoped lang="scss">
    .s-nav {
        width: 100%;
        box-shadow: 0 2px 0 #eee;
        padding-left: 50px;
        overflow: hidden;
    }
    
    .l-row {
        max-width: 83.33vw;
        min-width: 79.2vw;
        margin: 0 auto;
        position: relative;
    }
    
    .logo {
        height: 70px;
    }
    
    .title {
        font-size: 20px;
        color: #555;
        padding: 25px 0 15px;
    }
    
    .title_user {
        border-bottom: 3px solid #0095e2;
        margin-bottom: 50px;
    }
    
    h2 {
        margin-top: 20px;
        font-family: inherit;
        font-weight: 500;
        line-height: 1.1;
    }
    
    .mt40 {
        margin-top: 40px;
    }
    
    .el-button {
        width: 100%;
        border-radius: 0px;
        font-size: 16px;
        padding: 12px 20px !important;
    }
</style>